package com.fengjing.launcher.activity;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.percent.PercentRelativeLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.fengjing.launcher.R;
import com.fengjing.launcher.model.BackgroundModel;
import com.fengjing.launcher.parse.BackgroundParser;
import com.hg.library.Interface.SystemDevStateListener;
import com.hg.library.utils.SystemDevState;

/**
 * Created by admin on 2017/9/19.
 */

public class BaseActivity extends Activity {

    private ViewGroup view;

    public static BackgroundModel bgInfo = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(view = (ViewGroup) View.inflate(this, R.layout.activity_base, null));
        if (bgInfo == null){
            try {
                bgInfo = BackgroundParser.getBackgroundInfo(this);
            }catch (Exception e){
                bgInfo = new BackgroundModel();
                bgInfo.setBg(getDrawable(R.drawable.background));
            }
        }
        view.setBackground(bgInfo.getBg());
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {

        View inflate = View.inflate(this, layoutResID, null);
        PercentRelativeLayout.LayoutParams layoutParams = new PercentRelativeLayout.LayoutParams(-1, -1);
        inflate.setLayoutParams(layoutParams);
        view.addView(inflate);
    }

    @Override
    protected void onStart() {
        super.onStart();
        registerStateBar();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterStateBar();
    }

    @Override
    public void startActivity(Intent intent) {
        try {
            super.startActivity(intent);
            overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
        }catch (ActivityNotFoundException e){
            Toast.makeText(this, "应用不存在！！", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
    }

    private void registerStateBar() {
        View view = getStatusBarView();
        if (view != null) {
            SystemDevState.getInstance(this).registerSystemDevStateListener((SystemDevStateListener) view);
        }
    }

    private void unregisterStateBar() {
        if (getClass().getSimpleName().equals("MainActivity")) {
            SystemDevState.getInstance(this).unregisterSystemStateListener();
        }
    }


    public View getStatusBarView() {
        return findViewById(R.id.status_bar_view);
    }
}
